package tablet.module_tools.fragment;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.utils.px_dp.DisplayUtil;
import com.lib.ui.fragment.BaseAppFragment;
import com.luck.picture.lib.tools.ToastManage;

import java.util.List;

import butterknife.BindView;
import tablet.module_tools.R;

/**
 * Created by Administrator on 2020/7/13 0013.
 */

public class ToolsFragment extends BaseAppFragment {

    ImageView ivTool;

    @Override
    protected void onCreateView(View view, Bundle savedInstanceState) {

        int width = DisplayUtil.getScreenWidth(getActivity());
        int height = DisplayUtil.getScreenHeight(getActivity());

        //width = 1280-----------------height = 742
        LogUtil.e("width = " + width + "-----------------" + "height = " + height);

        ivTool = view.findViewById(R.id.iv_tool);
        ivTool.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                checkIsInstall(getActivity(), "com.jxw.launcher", "com.jxw.launcher.ThemeActivity");

//                if (!isAvilible("com.jxw.launcher", getActivity())) {
//
//                    showToast("您还没有安装AI同步课堂");
//                } else {

//                com.xueeryou.learningmachine.launcher4
//                com.xueeryou.learningmachine.launcher4.view.page.splash.SplashActivity

                //	com.jxw.launcher/com.jxw.launcher.ThemeActivity

                Intent intent = new Intent();
                //com.jxw.launcher----需要跳转应用的包名   com.jxw.launcher.ThemeActivity----需要跳转应用的指定页面
                ComponentName componentName = new ComponentName("com.jxw.launcher", "com.jxw.launcher.ThemeActivity");
                intent.setComponent(componentName);
                startActivity(intent);
//                }
            }
        });
    }

    /**
     * 检测
     */
    public static void checkIsInstall(Context ctx, String packageName, String activityName) {

        if (!isAvilible(packageName, ctx)) {
            //没有安装Apk
            ToastManage.s(ctx, "您还没有安装AI同步课堂,请安装后再使用");
            return;
        } else {

            //已经安装了Apk
            Intent intent = new Intent();
            intent.setClassName(packageName, activityName);
            ctx.startActivity(intent);
        }

    }


    /**
     * 检查是否安装了某应用
     *
     * @param packageName 包名
     * @return
     */
    public static boolean isAvilible(String packageName, Context mContext) {
        final PackageManager packageManager = mContext.getPackageManager();
        // 获取所有已安装程序的包信息
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        for (int i = 0; i < pinfo.size(); i++) {
            if (pinfo.get(i).packageName.equalsIgnoreCase(packageName))
                return true;
        }
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tools_1;
    }

}
